<%@ page import="dao.DaoException" %>
<%@ page import="dao.productDao.ProductDaoImpl" %>
<%@ page import="model.Product" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<style>
    .row-margin {
        margin-top: 20px;
    }
</style>
<!-- 主体 -->
<div style="margin-top: 10px" class="layui-container">
    <!-- 搜索 -->
    <form action="shopping.jsp" method="get">
        <div class="layui-row row-margin">
            <div style="float: right">
                搜索商品：
                <div class="layui-inline">
                    <input type="hidden" name="search" value="true">
                    <input
                            class="layui-input"
                            name="pattern"
                            id="demoReload"
                            autocomplete="off"
                    />
                </div>
                <button class="layui-btn" data-type="reload">搜索</button>
            </div>
        </div>
    </form>
    <!-- 搜索结束 -->

    <%
        String type = request.getParameter("type");
        String pattern = request.getParameter("pattern");
        String search = request.getParameter("search");
        if (type == null) {
            type = "手机数码";
        }
        int i = 0;
        ProductDaoImpl dao = new ProductDaoImpl();
        try {
            ArrayList<Product> products;
            if ("true".equals(search)) {
                products = dao.findProductsByPattern(pattern);
            } else {
                products = dao.findProductsByType(type);
            }
    %>
    <!-- 展示商品 -->
    <div class="layui-row">
        <!-- 第一行 -->
        <c:forEach var="product" items="<%=products%>">
            <%
                if (i++ % 3 == 0) {
            %>
            <div class="layui-row row-margin">
                <%}%>
                <!-- 第一个商品 -->
                <div class="layui-col-md4">
                    <!-- 第一列 -->
                    <div class="layui-col-md5">
                        <a href="/showProduct.jsp?id=${product.pid}"
                        ><img style="width: 130px" src="${product.imgURL}"
                        /></a>
                    </div>
                    <!-- 第二列 -->
                    <div class="layui-col-md5">
                        <ul>
                            <li>
                                <label>名称:</label>
                                    ${product.pname}
                            </li>
                            <li>
                                <label>价格:</label>
                                    ${product.price}
                            </li>
                            <li>
                                <label>类别:</label>
                                    ${product.type}
                            </li>
                            <li>
                                <label>库存:</label>
                                    ${product.stock}
                            </li>
                        </ul>
                    </div>
                </div>
                <%if (i % 3 == 0) {%>
            </div>
            <%}%>
        </c:forEach>

        <!-- 第二行 -->
    </div>
    <!-- 展示商品结束 -->
</div>
<%
    } catch (DaoException e) {
        e.printStackTrace();
    }
%>